<template>
  <div class="app-container">
    <el-dialog :title="titles[flag]" :visible.sync="dialogVisible" width="70%" append-to-body>
      <div v-loading="loading">
        <div class="myTitle">基本信息</div>
        <el-form ref="form" :model="form">
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="对账单号" prop="reconDocsNo">
                <div>{{ form.reconDocsNo || '-' }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="对账日期" prop="reconTime">
                <div>{{ form.reconTime || '-' }}</div>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="对账客户" prop="customerName" required>
                <div v-if="flag!=0">{{ form.customerName || '-' }}</div>
                <el-input v-else v-model="form.customerName" placeholder="请输入"
                          @blur="changeCust"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="总金额(元)" prop="income">
                <div>{{ form.income || '-' }}</div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="myTitle">对账项目</div>
      </div>
      <el-table
        :data="list"
        maxHeight="500"
        v-loading="loading"
      >
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="relationOrderNo" label="关联单据" align="center" width="180">
          <template slot-scope="scope">
            <el-button type="text" @click="goDetail(scope.row)">{{ scope.row.relationOrderNo }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="recipient" label="收件人" align="center" width="100"/>
        <el-table-column prop="goodsPrice" label="商品金额(元)" align="center">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.goodsPrice" placeholder="请输入"
                      v-if="flag==0||(flag==3&&form.reconDocsType==20)"></el-input>
            <div v-else>{{ scope.row.goodsPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="matPartPrice" label="配件金额(元)" align="center">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.matPartPrice" placeholder="请输入" :min="0"
                      v-if="flag==0||(flag==3&&form.reconDocsType==20)"></el-input>
            <div v-else>{{ scope.row.matPartPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="expressPrice" label="快递费用(元)" align="center">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.expressPrice" placeholder="请输入" :min="0"
                      v-if="flag==0||flag==2||(flag==3&&form.reconDocsType==20)"></el-input>
            <div v-else>{{ scope.row.expressPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="otherPrice" label="其它费用(元)" align="center">
          <template slot-scope="scope">
            <el-input type="number" v-model="scope.row.otherPrice" placeholder="请输入" :min="0"
                      v-if="flag==0||flag==2"></el-input>
            <div v-else>{{ scope.row.otherPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="totalPrice" label="总费用(元)" align="center" v-if="flag!=0">
          <template slot-scope="scope">
            <div v-if="flag==2"> {{ getPrice(scope.row) }}</div>
            <div v-else>{{ scope.row.totalPrice }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="financeStatus" label="财务审核" align="center" v-if="flag==1||flag==2||flag==3">
          <template slot-scope="scope">
            <el-select v-if="flag==3" v-model="scope.row.financeStatus">
              <el-option
                v-for="item in dict.type.sys_approve_result"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <div v-else>
              <el-tag type="primary" v-if="scope.row.financeStatus==0">通过</el-tag>
              <el-tag type="danger" v-else-if="scope.row.financeStatus==1">驳回</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="customerStatus" label="客户确认" align="center" v-if="flag==1||flag==2||flag==4">
          <template slot-scope="scope">
            <el-select v-if="flag==4" v-model="scope.row.customerStatus">
              <el-option
                v-for="item in dict.type.sys_approve_result"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <div v-else>
              <el-tag type="primary" v-if="scope.row.customerStatus==0">通过</el-tag>
              <el-tag type="danger" v-else-if="scope.row.customerStatus==1">驳回</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="saleStatus" label="销售审核" align="center" v-if="flag==1||flag==2">
          <template slot-scope="scope">
            <el-select v-if="flag==2" v-model="scope.row.saleStatus">
              <el-option
                v-for="item in dict.type.sys_approve_result"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <div v-else>
              <el-tag type="primary" v-if="scope.row.saleStatus==0">通过</el-tag>
              <el-tag type="danger" v-if="scope.row.saleStatus==1">驳回</el-tag>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag==0">确 定</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag==2">销售审核</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag==3">财务审核</el-button>
        <el-button type="success" @click="dialogOk" v-if="flag==4">客户确认</el-button>
      </div>
    </el-dialog>
    <detailDialog ref="detailDialog"></detailDialog>
    <orderDetailDialog ref="orderDetailDialog"></orderDetailDialog>
  </div>
</template>

<script>
import {
  addPayCheck,
  confirmPayCheck,
  okPayCheck,
  listCustomRelationOrder,
  okPayCheckFinance, getPayCheck
} from "../../../../api/payMng";
import {getCommonCode} from "../../../../api/imsMng";
import detailDialog from "./detailDialog.vue";
import orderDetailDialog from '../../orderMng/detailDialog.vue'

export default {
  components: {detailDialog, orderDetailDialog},
  dicts: ['sys_approve_result'],
  data() {
    return {
      flag: 0, //2审核，3确认
      titles: ['新增', '详情', '销售审核', '财务审核', '客户确认'],
      loading: false,
      dialogVisible: false,
      form: {},
      list: []
    }
  },
  methods: {
    show(flag, form) {
      this.dialogVisible = true
      this.flag = flag
      this.list = []
      this.form = {}
      this.resetForm('form')
      if (flag) {
        this.loading = true
        getPayCheck(form.id).then(res => {
          this.form = res.data
          this.list = res.data.items
        }).finally(e => {
          this.loading = false
        })
      } else {
        getCommonCode({orderType: 110}).then(res => {
          this.$set(this.form, 'reconDocsNo', res.data.orderNo)
        })
      }
    },
    getPrice(row) {
      let total = Number.parseFloat(row.goodsPrice) + Number.parseFloat(row.matPartPrice)
      total = total + Number.parseFloat(row.expressPrice) + Number.parseFloat(row.otherPrice)
      return total || 0
    },
    goDetail(row) {
      let req = {
        id: row.reOrderId
      }
      this.$refs.orderDetailDialog.show(req, 0)
    },
    changeCust() {
      this.list = []
      listCustomRelationOrder({customerName: this.form.customerName}).then(res => {
        let data = res.data
        if (data && data.length) {
          data.map(val => {
            this.list.push({
              relationOrderNo: val.orderNo
            })
          })
        }
      })
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.items = this.list
          if (this.flag == 0) {
            this.loading = true
            addPayCheck(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            }).finally(e => {
              this.loading = false
            })
          }
          if (this.flag == 2) {
            if (this.form.items && this.form.items.length) {
              let list = this.form.items.filter(e => !e.saleStatus)
              if (list && list.length) {
                this.$message.error('销售审核结果不能为空')
                return
              }
            }
            this.loading = true
            okPayCheck(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('销售审核成功')
            }).finally(e => {
              this.loading = false
            })
          }
          if (this.flag == 3) {
            if (this.form.items && this.form.items.length) {
              let list = this.form.items.filter(e => !e.financeStatus)
              if (list && list.length) {
                this.$message.error('财务审核结果不能为空')
                return
              }
            }
            this.loading = true
            okPayCheckFinance(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('财务审核成功')
            }).finally(e => {
              this.loading = false
            })
          }
          if (this.flag == 4) {
            if (this.form.items && this.form.items.length) {
              let list = this.form.items.filter(e => !e.customerStatus)
              if (list && list.length) {
                this.$message.error('客户确认结果不能为空')
                return
              }
            }
            this.loading = true
            confirmPayCheck(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('确认成功')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    }
  }
}
</script>
